package netWork;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-10-17
 * Time: 10:41
 */
public class TcpEchoClient {
    private Socket socket = null;

    public TcpEchoClient(String serverIp, int serverPort) throws IOException {
        socket = new Socket(serverIp,serverPort);
    }

    public void start() throws IOException {
        Scanner scanner = new Scanner(System.in);
        try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()) {
            PrintWriter printWriter = new PrintWriter(outputStream);
            //读取服务端的响应
            Scanner netScanner = new Scanner(inputStream);
            while (true) {
                System.out.print("->");
                //读取客户端请求
                String request = scanner.next();
                //将客户端请求发送到服务端
                printWriter.println(request);
                printWriter.flush();
                String response = netScanner.next();
                System.out.println(response);
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient = new TcpEchoClient("192.168.144.1", 9905);
        tcpEchoClient.start();
    }
}
